var result = 0;
var storeName = '';
var storeInfo = '';
var managerInfo = '';
var locationInfo = '';
var phoneInfo = '';
var emailInfo = '';
var validateInfo = '';

var inputStoreImage = document.getElementById("inputStoreImage");
var file;
function editItemImage(event){
    file = event.target.files[0] || event.dataTransfer.files[0] || this.file.files[0]
}

//初始化函数
function init(){
    loadStoreInfo();//请求后端用户店铺信息
    $("#editCardButton").click(function(){
        hideAlert('successAlert');
        hideAlert('failAlert');
    });
    $("#editInfoButton").click(function(){
        hideAlert('successAlert');
        hideAlert('failAlert');
    });
}

//获取用户店铺信息
function loadStoreInfo(){
    $.ajax({
        url:"http://localhost:8022/getIndexInfo",
        type:"GET",
        success:function (data){
            if(data !== ''){
                var datajson = JSON.parse(data);
                $("#storeImage").attr("src",datajson.imagesInfo);
                $("#StoreName").text(datajson.storeName);
                $("#StoreIntroduce").text(datajson.storeInfo);
                $("#managerInfo").text(datajson.managerInfo);
                $("#locationInfo").text(datajson.locationInfo);
                $("#phoneInfo").text(datajson.phoneInfo);
                $("#emailInfo").text(datajson.emailInfo);
                $("#validateInfo").text(datajson.validateInfo);
            }

        }
    });
}

//更新用户店铺名片(数据库)
function renewStoreCardInDataBase(newName,newInfo,new_description){
    var oldName = $("#StoreName").text();
    $.ajax({
        url:"http://localhost:8022/updateIndexStoreCard?"+"newName="+newName+"&newInfo="+newInfo+"&new_description="+new_description+"&oldName="+oldName,
        type:"POST",
        success:function(data){
            loadStoreInfo();
        },
        error:function (errorMsg){
            $("#failAlertText").text("更新店铺名片失败，请重式!");
            showAlert('failAlert');
        }
    })
}

//更新用户店铺详细信息(数据库)
function renewStoreInfoInDataBase(manager,location,phone,email,validateInfo){
    var storeName = $("#StoreName").text();
    $.ajax({
        url:"http://localhost:8022/updateIndexStoreInformation?"+"managerInfo="+manager+"&locationInfo="+location+"&phoneInfo="+phone+"&emailInfo="+email+"&validateInfo="+validateInfo+"&storeName="+storeName,
        type:"POST",
        success:function(data){
            loadStoreInfo();
        },
        error:function (errorMsg){
            $("#failAlertText").text("更新店铺详细信息失败，请重式!");
            showAlert('failAlert');
        }
    })
}

//提交店铺简介按钮点击事件
function onStoreCardSummit(){
    var name = $("#inputStoreName").val();
    var info = $("#inputStoreInfo").val();
    var formData = new FormData();
    var new_description = '/image/index/' + file.name;
    formData.append("file",file);

    $("#editCard").modal('hide');
    //如果提交信息为空
    if(name===''||info===''){
        $("#failAlertText").text("店铺名或简介不能为空!");
        showAlert('failAlert');
    }
    else{
        $.ajax({
            url:'http://localhost:8022/addIndexImage',
            type:'POST',
            data: formData,
            processData:false,
            contentType:false,
            cache:false,
            success:function (data){
                renewStoreCardInDataBase(name,info,new_description);
            },
            error:function (errorMsg) {
                $("#failAlertText").text("更新店铺名片失败，请重式!");
                showAlert('failAlert');
            }
        });
    }
}

//提交店铺详细信息按钮点击事件
function onConcreteInfoSummit(){
    var manager = $("#managerName").val();
    var location = $("#storeLocation").val();
    var phone = $("#storePhone").val();
    var email = $("#storeEmail").val();
    $("#editInfo").modal('hide');
    //如果提交信息为空
    if(manager===''||location===''||phone===''||email===''){
        $("#failAlertText").text("请完整填写店铺详细信息!");
        showAlert('failAlert');
    }
    else{
        renewStoreInfoInDataBase(manager,location,phone,email,'已认证');
    }
}

//显示信息框
function showAlert(alertId){
    $("#"+alertId).removeClass("invisible");
}

//隐藏信息框
function hideAlert(alertId){
    $("#"+alertId).addClass("invisible");
}

var logout = document.getElementById("btn-logout");

logout.addEventListener('click',onLogoutClick)

function onLogoutClick()
{
    console.log("logout")
    $.ajax(
        {
            type: 'GET',
            url: 'http://localhost:8022/logout',
            success: function (data)
            {
                console.log(data)
                if(data === 'success')
                {
                    window.location='http://localhost:8022/signIn'
                }
                else
                {
                    window.showAlert('注销失败')
                }
            },
            error: function (errorMag)
            {
                window.alert('注销失败')
                console.log(errorMag)
            }
        })
}